package com.wan.controller;


import com.wan.entity.LoginUser;
import com.wan.util.R;
import com.wan.service.IUserService;
import com.wan.service.TokenService;
import com.wan.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;


@RestController
public class LoginController {
	@Autowired
	private IUserService userService;
	@Autowired
	private TokenService tokenService;

	@GetMapping("/login")
	public R login(@RequestBody UserVo vo) {
		// 通过用户信息查找用户
		LoginUser loginUser = userService.findUser(vo);
		// 判断用户是否存在
		if (loginUser == null) {
			return R.fail("登录失败!!!");
		}
		// 生成 Token 并返回
		Map<String, Object> map = tokenService.createToken(loginUser);
		return R.ok(map, "登录成功!!！");
	}
}
